package one.stand.aspet;

import one.stand.aop.CompanyVerification;
import one.stand.model.ResultModel;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

/**
 * @author: QIK
 * @CreateDate: 2019/4/22 21:51
 */
@Aspect
@Component
public class CompanyVerificationAspet {

    private Logger logger = LoggerFactory.getLogger(CompanyVerificationAspet.class);

    @Around("pointcut()")
    public ResultModel before(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        proceedingJoinPoint.getTarget().getClass().getName();

        MethodSignature methodSignature = (MethodSignature) proceedingJoinPoint.getSignature();
        Method method = methodSignature.getMethod();
        CompanyVerification annotation = method.getAnnotation(CompanyVerification.class);
        //如果没有注解那么就放行
        if (null == annotation) {
           return (ResultModel) proceedingJoinPoint.proceed();
        }
        logger.info("开始检测公司是否存在。。。");

        //TODO 验证公司是否存在
        return (ResultModel) proceedingJoinPoint.proceed();
        //  return ResultModel.fail("公司不存在");
    }

    @Pointcut("@annotation(one.stand.aop.CompanyVerification)")
    public void pointcut() {
    }

}
